
#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.Forms;

#endregion
/** 
COPYRIGHT (C) 2008 Piyusha Desai. All Rights Reserved.
* Read Licence.txt before using this application.
*/
namespace SmsBuddy
{
    // all contact information here.
    
    public class ContactInfo : System.Windows.Forms.Form
    {
        private ListView lstContacts;
        private MenuItem menuDone;
        private MenuItem mainMenu;
        private MenuItem mnuRemoveContact;
        private MenuItem menuAddContact;
        private ColumnHeader columnHeader;
       
     
       
        private System.Windows.Forms.MainMenu mainMenu1;
        private ArrayList contactList = new ArrayList();

        Buddy buddy = null;
        private MenuItem menuItem1;
        Group group = null;

        public ContactInfo(Buddy buddy, Group group)
        {
            InitializeComponent();

            this.buddy = buddy;
            this.group = group;
        }

       
    
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

      
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
      
        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.menuDone = new System.Windows.Forms.MenuItem();
            this.mainMenu = new System.Windows.Forms.MenuItem();
            this.menuAddContact = new System.Windows.Forms.MenuItem();
            this.mnuRemoveContact = new System.Windows.Forms.MenuItem();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.lstContacts = new System.Windows.Forms.ListView();
            this.columnHeader = new System.Windows.Forms.ColumnHeader();
            this.SuspendLayout();
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.menuDone);
            this.mainMenu1.MenuItems.Add(this.mainMenu);
            // 
            // menuDone
            // 
            this.menuDone.Text = "Done";
            this.menuDone.Click += new System.EventHandler(this.menuDone_Click);
            // 
            // mainMenu
            // 
            this.mainMenu.MenuItems.Add(this.menuAddContact);
            this.mainMenu.MenuItems.Add(this.mnuRemoveContact);
            this.mainMenu.MenuItems.Add(this.menuItem1);
            this.mainMenu.Text = "Menu";
            // 
            // menuAddContact
            // 
            this.menuAddContact.Text = "Add Contact";
            this.menuAddContact.Click += new System.EventHandler(this.menuAddContact_Click);
            // 
            // mnuRemoveContact
            // 
            this.mnuRemoveContact.Enabled = false;
            this.mnuRemoveContact.Text = "Remove Contact";
            this.mnuRemoveContact.Click += new System.EventHandler(this.menuRemoveContact_Click);
            // 
            // menuItem1
            // 
            this.menuItem1.Text = "-";
            // 
            // lstContacts
            // 
            this.lstContacts.Columns.Add(this.columnHeader);
            this.lstContacts.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lstContacts.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
            this.lstContacts.Location = new System.Drawing.Point(0, 0);
            this.lstContacts.Name = "lstContacts";
            this.lstContacts.Size = new System.Drawing.Size(240, 266);
            this.lstContacts.TabIndex = 0;
            this.lstContacts.View = System.Windows.Forms.View.Details;
            this.lstContacts.ItemActivate += new System.EventHandler(this.lstContacts_ItemActivate);
            // 
            // columnHeader
            // 
            this.columnHeader.Text = "Contact Name";
            this.columnHeader.Width = 129;
            // 
            // ContactInfo
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(240, 266);
            this.Controls.Add(this.lstContacts);
            this.Menu = this.mainMenu1;
            this.Name = "ContactInfo";
            this.Text = "Contacts in Group";
            this.Load += new System.EventHandler(this.ContactInfo_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private void ContactInfo_Load(object sender, EventArgs e)
        {
          
            columnHeader.Width = lstContacts.Width - 20;

            RefreshContacts();

        }

        private void RefreshContacts()
        {
            ContactCollection contacts = group.GetContacts();
            if (contacts != null)
            {
                lstContacts.Items.Clear();
                contactList.Clear();

                if (contacts != null)
                {
                    foreach (Contact contact in contacts)
                    {
                        AddContactToUi(contact);
                    }
                }
            }
        }

        private void AddContactToUi(Contact contact)
        {

            lstContacts.Items.Add(new ListViewItem(contact.FileAs));

           contactList.Add(contact);
        }


        private void menuDone_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menuAddContact_Click(object sender, EventArgs e)
        {
            Contact contact = group.AddContact();
            if (contact != null)
            {
                AddContactToUi(contact);
            }
        }

        private void menuRemoveContact_Click(object sender, EventArgs e)
        {
            RemoveSelectedContact();


            
        }

        private void RemoveSelectedContact()
        {
            Contact contact = GetSelectedContact();
            contact.Properties[Buddy.ContactPropertyName] = 0;
            contact.Update();

            RefreshContacts();
        }

        private Contact GetSelectedContact()
        {
            Contact contact = (Contact)contactList[lstContacts.SelectedIndices[0]];
            return contact;
        }

        private void lstContacts_ItemActivate(object sender, EventArgs e)
        {
            Contact contact = GetSelectedContact();
            contact.ShowDialog();
        }              

    }
}
